package workspace.teaweb.Merchant.controllers;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import workspace.teaweb.Merchant.pojo.dto.MerchantAddParam;
import workspace.teaweb.Merchant.pojo.dto.MerchantQuery;
import workspace.teaweb.Merchant.pojo.dto.MerchantUpdateParam;
import workspace.teaweb.Merchant.pojo.vo.MerchantVO;
import workspace.teaweb.Merchant.service.MerchantService;
import workspace.teaweb.base.response.JsonResult;

import java.util.List;


@Slf4j
@RestController // @Controller + @ResponseBody
@RequestMapping("v1/merchant")
public class MerchantController {
    @Autowired
    MerchantService merchantService;

    /*1.添加商品*/
    @PostMapping("add")
    public JsonResult addMerchant(@RequestBody MerchantAddParam abc){
       log.debug("添加商品信息:{}",abc);
        merchantService.addMerchant(abc);
        return JsonResult.ok("添加成功");
    }

    /*2.查询商品(根据商品名模糊查询)*/
    @GetMapping("select")
    public JsonResult selectMerchant(MerchantQuery merchantQuery){
        log.debug("要查询的商品名称:{}",merchantQuery);
        System.out.println(merchantQuery);
        List<MerchantVO> list = merchantService.selectMerchant(merchantQuery);
        System.out.println(list);
        //返回的是一个MerchantVO类的集合
        return JsonResult.ok(list);
    }
//    /*3.根据商户id查询商品，查商户自己添加的商品*/
//    @GetMapping("selectByUserId")
//    public JsonResult selectMerchantByUserId(Integer userId){
//        log.debug("要查询的商户id:{}",userId);
//        List<MerchantVO> list = merchantService.selectMerchantByUserId(userId);
//        //返回的是一个MerchantVO类的集合
//        return JsonResult.ok(list);
//    }

    /*4.商品的更新(只有商户可以操作)，名称，规格，价格，图片链接，额外信息可改*/
    @PostMapping("update")
    public JsonResult updateMerchant(@RequestBody MerchantUpdateParam newMerchantMessage){
        log.debug("要修改的商品信息:{}",newMerchantMessage);
        merchantService.updateMerchant(newMerchantMessage);
        return JsonResult.ok("更新成功");
    }

    /*5.删除商品*/
    @PostMapping("/delete/{id}")
    public JsonResult deleteMerchant(@PathVariable Long id){
        log.debug("要删除的商品id:{}",id);
        merchantService.deleteMerchant(id);
        return JsonResult.ok();
    }





}
